package com.example.eureka_client.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eureka_client.entity.Answer;
import com.example.eureka_client.entity.AnswerRelation;
import com.example.eureka_client.entity.Question;
import com.example.eureka_client.mapper.AnswerMapper;
import com.example.eureka_client.mapper.AnswerRelationMapper;
import com.example.eureka_client.mapper.QuestionMapper;
import com.example.eureka_client.service.IAnswerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eureka_client.utils.PageRequest;
import com.example.eureka_client.utils.PageResult;
import com.example.eureka_client.utils.PageUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 问题回答 服务实现类
 * </p>
 *
 * @author plj
 * @since 2022-03-24
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {
    @Resource
    AnswerRelationMapper answerRelationMapper;


    private final static SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public PageResult getAnswersByQuestionId(String questionId, PageRequest pageRequest) {
        List<Answer> answers = baseMapper.getAnswersByQuestionId(questionId);
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        Page page = new Page(pageNum, pageSize);
        int total = answers.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);

        if (startIndex > endIndex) {
            page.addAll(new ArrayList());
            PageInfo pageInfo = new PageInfo<>(page);
            return PageUtils.getPageResult(pageRequest,pageInfo);
        } else {
            page.addAll(answers.subList(startIndex, endIndex));
            PageInfo pageInfo = new PageInfo<>(page);
            return PageUtils.getPageResult(pageRequest,pageInfo);
        }
    }

    @Override
    public boolean addAnswer(Answer answer){
        answer.setId(UUID.randomUUID().toString());
        Date nowTime = new Date();
        answer.setCreateTime(sdf.format(nowTime));
        return this.save(answer);
    }

    @Override
    public boolean editAnswer(String answerId, String content){
        Date nowTime = new Date();

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);
        updateWrapper.set("UPDATE_TIME", sdf.format(nowTime));

        if(!content.equals("")) updateWrapper.set("CONTENT", content);



        if(this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean delAnswer(String answerId){
        return baseMapper.deleteById(answerId);
    }

    @Override
    public boolean addNotAgreeNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("AGREE_NUM", answer.getAgreeNum() + 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(1);

        if(answerRelationMapper.insert(answerRelation) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean addAgreeNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("NOT_AGREE_NUM", answer.getNotAgreeNum() + 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(0);

        if(answerRelationMapper.insert(answerRelation) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean addLikeNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("LIKE_NUM", answer.getLikeNum() + 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(3);

        if(answerRelationMapper.insert(answerRelation) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean addCllectionNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("COMMENT_NUM", answer.getLikeNum() + 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(2);

        if(answerRelationMapper.insert(answerRelation) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reNotAgreeNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("AGREE_NUM", answer.getAgreeNum() - 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(1);

        if(answerRelationMapper.deleteById(answerRelation.getAnswerId(), answerRelation.getUserId(), answerRelation.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reAgreeNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("NOT_AGREE_NUM", answer.getNotAgreeNum() - 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(0);

        if(answerRelationMapper.deleteById(answerRelation.getAnswerId(), answerRelation.getUserId(), answerRelation.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reLikeNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("LIKE_NUM", answer.getLikeNum() - 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(3);

        if(answerRelationMapper.deleteById(answerRelation.getAnswerId(), answerRelation.getUserId(), answerRelation.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reCllectionNum(String answerId, String userId){
        Answer answer = baseMapper.getById(answerId);

        UpdateWrapper<Answer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", answerId);

        updateWrapper.set("COMMENT_NUM", answer.getLikeNum() - 1);

        AnswerRelation answerRelation = new AnswerRelation();
        answerRelation.setAnswerId(answerId);
        answerRelation.setUserId(userId);
        answerRelation.setType(2);

        if(answerRelationMapper.deleteById(answerRelation.getAnswerId(), answerRelation.getUserId(), answerRelation.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean notAgree(String answerId, String userId){
        AnswerRelation answerRelation = answerRelationMapper.getBy(answerId, userId, 1);
        if(answerRelation == null) return false;
        else return true;
    }

    @Override
    public boolean agree(String answerId, String userId){
        AnswerRelation answerRelation = answerRelationMapper.getBy(answerId, userId, 0);
        if(answerRelation == null) return false;
        else return true;
    }

    @Override
    public boolean like(String answerId, String userId){
        AnswerRelation answerRelation = answerRelationMapper.getBy(answerId, userId, 3);
        if(answerRelation == null) return false;
        else return true;
    }

    @Override
    public boolean cllection(String answerId, String userId){
        AnswerRelation answerRelation = answerRelationMapper.getBy(answerId, userId, 2);
        if(answerRelation == null) return false;
        else return true;
    }
}
